﻿//------------------------------------------------------------------------------
//  
//------------------------------------------------------------------------------
#region Imported Namespaces
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;

using SSRS;
using PHSRAG.Insight21.Reports;
using PHSRAG.Insight21.InsightUtilities;
using Utils = PHSRAG.Insight21.InsightUtilities;
using PHSRAG.Insight21.SearchContext;
//using UserContext = PHSRAG.Insight21.Context.UserContext;
using AgreementsConstants = PHSRAG.Insight21.InsightUtilities.Constants.AgreementsConstants;

#endregion

namespace PHSRAG.Insight21.Agreements.Reports {

    
    public class sqlSalaryReport : sqlReport
    {
        private static string SearchCriteriaVersionKey = "SearchCriteriaVersion_71E2ECBB-0308-45bc-8986-7D30ADEF3022";
        private static string IncomingFundNumberKey = "IncomingFundNumberKey_71E2ECBB-0308-45bc-8986-7D30ADEF3022";
        private bool isDetail = false;
        private const string AsofDateKey = "glPostAsOfDate";

        public sqlSalaryReport(string pageUrl) : base(pageUrl)
        {
            doCustomExcelReport = true;
        }
        
        public override string ReportName {
            get {
                return "SalaryDetailsReport";
            }
           
        }

        public override ParameterValue[] setParameters(ref string rptName)
        {
            Agreements.SalaryDistributions salaryDistributions = SalaryDistributions;
            reportSource = salaryDistributions.GetDataContainer();

            Hashtable h2 = cachePolicy.GetCachedObject(AgreementsConstants.AgreementDetailsKey) as Hashtable;

            if (h2 != null)
            {
                reportTitle = "Salary Details";
                rptName = rptFormat=="Excel"?"Agmt_SalaryDetailsReportExcel":"Agmt_SalaryDetailsReport";
                reportResultCount = rptSource.Tables[1].Rows.Count.ToString();
                
            }
            else
            {
                reportTitle = "Salary Overview";
                rptName = "Agmt_SalariesOverviewReport";
                reportResultCount = rptSource.Tables[0].Rows.Count.ToString();
            }

            return base.setParameters(ref rptName);
        }
 
        private int SearchCriteriaVersion
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.SalariesOverviewPageKey) as Hashtable;
                return (h == null || h[SearchCriteriaVersionKey] == null) ? 0 : (int)h[SearchCriteriaVersionKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.SalariesOverviewPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[SearchCriteriaVersionKey] = value;
                pageCachePolicy.Cache(Keys.SalariesOverviewPageKey, h);
            }
        }

        private string ReferingSummaryPage
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.SalariesOverviewPageKey) as Hashtable;
                return (h == null || h[Keys.SummaryPageReferrerKey] == null) ? string.Empty : (string)h[Keys.SummaryPageReferrerKey];
            }
        }

        private string IncomingFundNumber
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.SalariesOverviewPageKey) as Hashtable;
                return (h == null || h[IncomingFundNumberKey] == null) ? string.Empty : h[IncomingFundNumberKey].ToString();
            }
        }

        /// <summary>
        /// Get the Salary Distributions that the logged-in user has access to. The list of Salary Distributions are 
        /// maintained by the SalaryDistributions business object within its data container. The business object is cached.
        /// Based on Summary pages that was visited first set FundStatus and AgreementStatus keys,
        /// if comming from AgreementSummary set the FundStatusId key to 0, default to bring all funds
        /// and if comming from financialSummay set the AgreementStatusId key to "", db gets all agreements for this Search key.
        /// </summary>
        private Agreements.SalaryDistributions SalaryDistributions
        {
            get
            {
                int searchVersion = searchCriteria.Version;

                Agreements.SalaryDistributions salaryDistributions = null;

                if ((searchContext != null) && (searchVersion == SearchCriteriaVersion) && (searchVersion != -1))
                {
                    salaryDistributions =
                  Policy.ResultsCache.GetObject(cachePolicy, Agreements.SalaryDistributions.CacheKey) as Agreements.SalaryDistributions;
                }

                if (salaryDistributions == null)
                {
                    salaryDistributions = new Agreements.SalaryDistributions();

                    bool ignoreFundStatus = false;
                    bool ignoreAgreementStatus = false;
                    // Based on Summary pages that was visited first set FundStatus and AgreementStatus keys,
                    // if comming from AgreementSummary set the FundStatusId key to 0, default to bring all funds
                    // and if comming from financialSummay set the AgreementStatusId key to "", db gets all agreements for this Search key.
                    if (ReferingSummaryPage == InsightUtilities.Constants.ModulePages.AgreementSummary)
                    {
                        //if the fund status isn't is search criteria ignore it
                        if (searchCriteria[InsightUtilities.Constants.FundStatusIDKey] == null)
                            ignoreFundStatus = true;
                    }
                    else if (ReferingSummaryPage == InsightUtilities.Constants.ModulePages.FinancialSummary)
                    {
                        if (searchCriteria[InsightUtilities.Constants.AgreementStatusIDKey] == null)
                            ignoreAgreementStatus = true;
                    }

                    salaryDistributions.Load(
                        Utility.DataAccess.GetConnectionString(), Session.SessionID
                        , searchCriteria, searchCriteria[AsofDateKey].ToString()       //refreshDates.GLPostAsOfDate.ToShortDateString()
                        , IncomingFundNumber, ignoreFundStatus, ignoreAgreementStatus, isDetail);

                    SearchCriteriaVersion = searchVersion;

                    Policy.ResultsCache.SetObject(cachePolicy, Agreements.SalaryDistributions.CacheKey, salaryDistributions);
                }

                return salaryDistributions;
            }
        }

  
    }
}
